<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" />
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <style>
        .layui-table{width: 100% !important;}
        .table-btn {height: 26px;line-height: 26px;}
        .table-btn+.table-btn {
            margin-left: 10px;
        }
        .form-style{padding-right: 50px;padding-bottom: 50px;padding-top: 20px;box-sizing: border-box;position: relative}
        .hide {display: none;}
    </style>
</head>
<body>
<div class="layui-fluid">
    <form class='layui-form' id="formData"></form>
    <table class="layui-hide" id="data-table" lay-filter="data-table">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn pear-btn pear-btn-primary pear-btn-sm"  lay-event="search" >刷新</button>
                <button class="layui-btn pear-btn pear-btn-primary pear-btn-sm" lay-event="execute" >立即执行</button>
                <button class="layui-btn pear-btn pear-btn-primary pear-btn-sm"  lay-event="clearAll" >清空日志</button>
            </div>
        </script>

        <script type="text/html" id="return_code">
            {{#  if(d.return_code == 0){ }}
            <button type="button" class="layui-btn layui-btn-sm ">成功</button>
            {{#  } else { }}
            <button type="button" class="layui-btn layui-btn-sm  layui-btn-danger">失败</button>
            {{#  } }}
        </script>
    </table>
</div>

<script>
    layui.use(['table'],function(){
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;


        // 渲染表格
        function render()
        {
            table.render({
                elem: '#data-table'
                , url: '/app/tuCrontabs/Crontabs/getLog'+location.search
                , toolbar: '#toolbarDemo'
                , method: "get",
                cellMinWidth: 80 //全局定义常规单元格的最小宽度
                , cols: [[
                    // {field: 'id', title: 'ID', width: 80}
                     {field: 'return_code', title: '状态', templet: '#return_code', width: 80}
                    , {field: 'target', title: '调用目标',width:200}
                    , {field: 'running_time', title: '执行耗时', width: 120}
                    , {field: 'log', title: '执行日志'}
                    , {field: 'create_time', title: '执行时间', sort: true, width: 165}
                ]]
                , page: true,
            });

        }

        // 添加 批量删除 刷新事件
        table.on("toolbar(data-table)", function(obj) {
            let params = getUrlParams(obj.config.url);
            if (obj.event === "search") {
                table.reload("data-table");
            } else if (obj.event === "execute") {
                // 立即执行
                $.get('/app/tuCrontabs/Crontabs/runOne',{id:params.id},function(res){
                    if(res.code==1){
                        layer.msg(res.msg,{icon:5});

                    }else{
                        layer.msg(res.msg,{icon:1});
                        table.reload("data-table");
                    }
                })
            } else if (obj.event === "clearAll") {
                // 全删
                $.get('/app/tuCrontabs/Crontabs/deleteLog',{id:params.id},function(res){
                    if(res.code==0){
                        layer.msg(res.msg,{icon:1});
                        table.reload("data-table");
                    }
                })
            }
        });

        // 渲染表格
        render();

    })

    // 执行删除
    let doRemove = function (ids, obj) {
        let data = {};
        data[PRIMARY_KEY] = ids;
        layer.confirm("确定删除?", {
            icon: 3,
            title: "提示"
        }, function(index) {
            layer.close(index);
            let loading = layer.load();
            $.ajax({
                url: DELETE_API,
                data: data,
                dataType: "json",
                type: "post",
                success: function(res) {
                    layer.close(loading);
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功", function () {
                        return obj ? obj.del() : refreshTable();
                    });
                }
            })
        });
    }


    function getUrlParams(url) {
        const paramsRegex = /[?&]+([^=&]+)=([^&]*)/gi;
        const params = {};
        let match;
        while (match = paramsRegex.exec(url)) {
            params[match[1]] = match[2];
        }
        return params;
    }

</script>

</body>
</html>
